package cn.wolfcode.shop.web.resolver;

import cn.wolfcode.shop.domain.User;
import cn.wolfcode.shop.redis.UserKeyPrefix;
import cn.wolfcode.shop.service.ISeckillUserService;
import cn.wolfcode.shop.web.util.CookieUtil;
import cn.wolfcode.shop.web.util.UserContext;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class UserArgumentsResolver implements HandlerMethodArgumentResolver {
    @Reference
    private ISeckillUserService seckillUserService;

    /**
     * 支持拦截的参数  如果参数类型为User类型  则进行拦截处理
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType() == User.class;
    }

    /**
     * 对参数拦截处理,返回值将注入被拦截的参数中
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return UserContext.get();
    }
}
